import socket
import fcntl
import struct
import os
import re

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

##Build list of files with ##HOST## 
ipaddr = get_ip_address('eth0')
filesInDir=os.listdir(os.getcwd())
for fname in filesInDir:
  if not os.path.isdir(fname):
    origText = open(fname).read()
    replaceText = origText.replace('##HOST##',ipaddr)
    if origText != replaceText and fname != 'updatehosts.py':
      print "HOST found in " + fname  
      f = open(fname,'w')
      f.write(replaceText)
      f.close()
